<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="订单ID" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物流单号" prop="logsNumber">
        <el-input
          v-model="queryParams.logsNumber"
          placeholder="请输入物流单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物流公司ID" prop="companyId">
        <el-select v-model="queryParams.companyId" clearable placeholder="请选择">
          <el-option
            v-for="item in companyList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="送件人ID" prop="postmanId">
        <el-select v-model="queryParams.postmanId" clearable placeholder="请选择">
          <el-option
            v-for="item in postmanList"
            :key="item.userId"
            :label="item.name"
            :value="item.userId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="物流状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择物流状态" clearable>
          <el-option
            v-for="dict in dict.type.log_status_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发货地址" prop="shippingAddress">
        <el-input
          v-model="queryParams.shippingAddress"
          placeholder="请输入发货地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货地址" prop="deliveryAddress">
        <el-input
          v-model="queryParams.deliveryAddress"
          placeholder="请输入收货地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="详细收货地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入详细收货地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发货时间" prop="deliveryTime">
        <el-date-picker clearable
                        v-model="queryParams.deliveryTime"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm"
                        :picker-options="pickerOptions"
                        placeholder="请选择发货时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="预计到达时间" prop="estimatedTime">
        <el-date-picker clearable
                        v-model="queryParams.estimatedTime"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm"
                        :picker-options="pickerOptions"
                        placeholder="请选择预计到达时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="实际到达时间" prop="actualTime">
        <el-date-picker clearable
                        v-model="queryParams.actualTime"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm"
                        :picker-options="pickerOptions"
                        placeholder="请选择实际到达时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['logistics:logistics:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['logistics:logistics:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['logistics:logistics:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['logistics:logistics:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="logisticsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50px" align="center"/>
      <el-table-column label="物流信息ID" width="85px" align="center" prop="id"/>
      <el-table-column label="订单" align="center" prop="orderId" width="150px">
        <template slot-scope="scope">
          <el-button @click="getOrderById('订单详细信息',scope.row.orderId)" type="primary" style="width: 140px">
            查看订单详情
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="物流单号" width="75px" align="center" prop="logsNumber"/>
      <el-table-column label="物流公司" align="center" prop="companyId" width="150px">
        <template slot-scope="scope">
          <el-button @click="getCompanyById('物流公司详情',scope.row.companyId)" type="primary" style="width: 140px">
            物流公司详情
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="送件人" align="center" prop="postmanId" width="150px">
        <template slot-scope="scope">
          <el-button @click="getPostmanById('送件人详情',scope.row.postmanId)" type="primary" style="width: 140px">
            送件人详情
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="物流状态" align="center" prop="status" width="100px">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.log_status_type" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="发货地址" align="center" prop="shippingAddress" width="100px"/>
      <el-table-column label="收货地址" align="center" prop="deliveryAddress" width="100px"/>
      <el-table-column label="详细收货地址" align="center" prop="address" width="100px"/>
      <el-table-column label="发货时间" align="center" prop="deliveryTime" width="100px">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliveryTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预计到达时间" align="center" prop="estimatedTime" width="100px">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.estimatedTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际到达时间" align="center" prop="actualTime" width="100px">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.actualTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['logistics:logistics:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['logistics:logistics:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--  抽屉显示对应id的详细信息  -->
    <el-drawer
      :title="drawerTitle"
      :visible.sync="drawer"
      size="30%"
    >
      <div v-if="drawerTitle==='订单详细信息'" class="订单">
        <el-descriptions :column="1" border class="a1" style="margin-left: 10px">
          <el-descriptions-item>
            <template slot="label"><i class="el-icon-grape"></i>订单编号</template>
            {{ query.orderId }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label"><i class="el-icon-shopping-bag-1"></i>商铺:</template>
            <el-button type="success" plain @click="getShopsById('商铺详情',query.shopsId)">查看详情</el-button>
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label"><i class="el-icon-shopping-cart-1"></i>商品:</template>
            <el-button type="warning" plain @click="getProById('商品详情',query.productId)">查看详情</el-button>
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label"><i class="el-icon-s-custom"></i>用户:</template>
            <el-button type="primary" plain @click="getUserById('用户详情',query.userId)">查看详情</el-button>
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label"><i class="el-icon-s-flag"></i>优惠:</template>
            <el-button type="danger" plain @click="getCouponById('优惠详情',query.couponId)">查看详情</el-button>
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label"><i class="el-icon-more"></i>商品数量</template>
            {{ query.productCount }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label"><i class="el-icon-bank-card"></i>支付方式</template>
            {{ query.payType }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label"><i class="el-icon-s-promotion"></i>配送方式</template>
            {{ query.sendType }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label"><i class="el-icon-s-check"></i>物流单号</template>
            {{ query.sendId }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label"><i class="el-icon-s-home"></i>收货地址</template>
            {{ query.receiveLocal }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label"><i class="el-icon-s-order"></i>用户留言</template>
            {{ query.userComment }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label"><i class="el-icon-coin"></i>应付金额</template>
            {{ query.meet }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label"><i class="el-icon-wallet"></i>实付金额</template>
            {{ query.actualPayment }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label"><i class="el-icon-chat-line-round"></i>订单状态</template>
            {{ query.status }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label"><i class="el-icon-alarm-clock"></i>支付时间</template>
            {{ query.payTime }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label"><i class="el-icon-timer"></i>发货时间</template>
            {{ query.deliveryTime }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label"><i class="el-icon-watch-1"></i>收货时间</template>
            {{ query.receivingTime }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label"><i class="el-icon-edit"></i>修改人</template>
            {{ query.modifiedBy }}
          </el-descriptions-item>
        </el-descriptions>
      </div>
      <div v-if="drawerTitle==='物流公司详情'" class="订单">
        <el-descriptions :column="1" border class="a1" style="margin-left: 10px">
          <el-descriptions-item>
            <template slot="label"><i class="el-icon-truck"></i>物流公司</template>
            {{ query.name }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label"><i class="el-icon-picture-outline-round"></i>商标</template>
            <image-preview :src="query.image" :width="100" :height="100" class="drawer"/>
          </el-descriptions-item>
        </el-descriptions>
      </div>
      <div v-if="drawerTitle==='送件人详情'" class="订单">
        <el-descriptions :column="1" border class="a1" style="margin-left: 10px">
          <el-descriptions-item>
            <template slot="label"><i class="el-icon-info"></i>设备ID</template>
            {{ query.deviceId }}
          </el-descriptions-item><el-descriptions-item>
          <template slot="label"><i class="el-icon-eleme"></i>真实姓名</template>
          {{ query.name }}
        </el-descriptions-item><el-descriptions-item>
          <template slot="label"><i class="el-icon-phone-outline"></i>电话号码</template>
          {{ query.phone }}
        </el-descriptions-item>
        </el-descriptions>
      </div>
    </el-drawer>

    <!--  商铺详情抽屉  -->
    <el-drawer
      v-if="innerDrawerTitle==='商铺详情'"
      :title="innerDrawerTitle"
      :visible.sync="innerDrawer"
      :append-to-body="true"
      size="20%">
      <el-descriptions :column="1" border class="a1" style="margin-left: 10px">
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-pear"></i>
            商铺ID
          </template>
          {{ innerQuery.shopsId }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-shopping-cart-2"></i>
            店铺名称
          </template>
          {{ innerQuery.shopName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-s-custom"></i>
            店长
          </template>
          {{ innerQuery.shopUser }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-picture-outline"></i>
            店家Logo
          </template>
          <image-preview :src="innerQuery.shopLogo" :width="50" :height="50"/>
        </el-descriptions-item>
      </el-descriptions>
    </el-drawer>
    <!--  商品详情抽屉  -->
    <el-drawer
      v-if="innerDrawerTitle==='商品详情'"
      :title="innerDrawerTitle"
      :visible.sync="innerDrawer"
      :append-to-body="true"
      size="20%">
      <el-descriptions :column="1" border class="a1" style="margin-left: 10px">
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-sugar"></i>
            ID
          </template>
          {{ innerQuery.proId }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-apple"></i>
            编号
          </template>
          {{ innerQuery.proNumber }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-money"></i>
            价格
          </template>
          {{ innerQuery.proPrice }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-s-order"></i>
            名称
          </template>
          {{ innerQuery.proName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-picture"></i>
            样图
          </template>
          <image-preview :src="innerQuery.image" :width="50" :height="50"/>
        </el-descriptions-item>
      </el-descriptions>
    </el-drawer>
    <!--  用户详情抽屉  -->
    <el-drawer
      v-if="innerDrawerTitle==='用户详情'"
      :title="innerDrawerTitle"
      :visible.sync="innerDrawer"
      :append-to-body="true"
      size="20%">
      <el-descriptions :column="1" border class="a1" style="margin-left: 10px">
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-ice-cream-round"></i>
            ID
          </template>
          {{ innerQuery.userId }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-user"></i>
            账号
          </template>
          {{ innerQuery.userName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-user-solid"></i>
            昵称
          </template>
          {{ innerQuery.nickName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-paperclip"></i>
            邮箱
          </template>
          {{ innerQuery.email }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-phone"></i>
            电话
          </template>
          {{ innerQuery.phonenumber }}
        </el-descriptions-item>
      </el-descriptions>
    </el-drawer>
    <!--  优惠券详情抽屉-->
    <el-drawer
      v-if="innerDrawerTitle==='优惠详情'"
      :title="innerDrawerTitle"
      :visible.sync="innerDrawer"
      :append-to-body="true"
      size="20%">
      <el-descriptions :column="1" border class="a1" style="margin-left: 10px">
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-cherry"></i>
            ID
          </template>
          {{ innerQuery.couponId }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-collection"></i>
            名称
          </template>
          {{ innerQuery.name }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-share"></i>
            类型
          </template>
          {{ innerQuery.type }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-s-ticket"></i>
            面额
          </template>
          {{ innerQuery.face }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-s-shop"></i>
            满额
          </template>
          {{ innerQuery.fullAmount }}
        </el-descriptions-item>
      </el-descriptions>
    </el-drawer>

    <!-- 添加或修改物流信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" inline="inline" label-position="left">
        <el-form-item label="订单ID" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单ID"/>
        </el-form-item>
        <el-form-item label="物流单号" prop="logsNumber">
          <el-input v-model="form.logsNumber" placeholder="请输入物流单号"/>
        </el-form-item>
        <el-form-item label="物流公司ID" prop="companyId">
          <el-select v-model="form.companyId" clearable placeholder="请选择">
            <el-option
              v-for="item in companyList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="送件人ID" prop="postmanId">
          <el-select v-model="form.postmanId" clearable placeholder="请选择">
            <el-option
              v-for="item in postmanList"
              :key="item.userId"
              :label="item.name"
              :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="物流状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择物流状态">
            <el-option
              v-for="dict in dict.type.log_status_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发货地址" prop="shippingAddress">
          <el-input v-model="form.shippingAddress" placeholder="请输入发货地址"/>
        </el-form-item>
        <el-form-item label="收货地址" prop="deliveryAddress">
          <el-input v-model="form.deliveryAddress" placeholder="请输入收货地址"/>
        </el-form-item>
        <el-form-item label="详细收货地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细收货地址"/>
        </el-form-item>
        <el-form-item label="发货时间" prop="deliveryTime">
          <el-date-picker clearable
                          v-model="form.deliveryTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm"
                          :picker-options="pickerOptions"
                          placeholder="请选择物流信息改变时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预计到达时间" prop="estimatedTime">
          <el-date-picker clearable
                          v-model="form.estimatedTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm"
                          :picker-options="pickerOptions"
                          placeholder="请选择物流信息改变时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="实际到达时间" prop="actualTime">
          <el-date-picker clearable
                          v-model="form.actualTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm"
                          :picker-options="pickerOptions"
                          placeholder="请选择物流信息改变时间">
          </el-date-picker>
        </el-form-item>
        <el-divider content-position="center">物流详细信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddInfo">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteInfo">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="infoList" :row-class-name="rowInfoIndex" @selection-change="handleInfoSelectionChange"
                  ref="info">
          <el-table-column type="selection" width="50" align="center"/>
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="物流单号" prop="logsNumber" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.logsNumber" placeholder="请输入物流单号"/>
            </template>
          </el-table-column>
          <el-table-column label="物流信息改变时间" prop="changeTime" width="200">
            <template slot-scope="scope">
              <el-date-picker clearable
                              v-model="scope.row.changeTime"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm"
                              :picker-options="pickerOptions"
                              placeholder="请选择物流信息改变时间">
              </el-date-picker>
            </template>
          </el-table-column>
          <el-table-column label="物流状态" prop="status" width="120">
            <template slot-scope="scope">
              <el-select v-model="scope.row.status" placeholder="请选择物流状态">
                <el-option
                  v-for="dict in dict.type.log_status_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="运输中状态" prop="transitStatus" width="120">
            <template slot-scope="scope">
              <el-select v-model="scope.row.transitStatus" placeholder="请选择运输中状态">
                <el-option
                  v-for="dict in dict.type.log_transit_status_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="当前所在地址ID" prop="currentAddress" width="180">
            <template slot-scope="scope">
              <el-cascader
                style="width: 100%;"
                :options="regions"
                :value="scope.row.currentParent"
                :show-all-levels="false"
                :props="{
                      value:'id',
                      label:'name',
                  }"
                @change="handleCascaderChange(scope.row, $event)"
                clearable>
              </el-cascader>
            </template>
          </el-table-column>
          <el-table-column label="发往地址ID" prop="toAddress" width="180">
            <template slot-scope="scope">
                <el-cascader
                  style="width: 100%;"
                  :options="regions"
                  :value="scope.row.toParent"
                  :show-all-levels="false"
                  :props="{
                      value:'id',
                      label:'name',
                  }"
                  @change="handleCascaderChange(scope.row, $event)"
                  clearable>
                </el-cascader>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listLogistics,
  getLogistics,
  delLogistics,
  addLogistics,
  updateLogistics,
  getLogisticsCascader
} from "@/api/logistics/logistics";
import {listOrder, getOrder, delOrder, addOrder, updateOrder} from "@/api/cart/order";
import {listCompany, getCompany, delCompany, addCompany, updateCompany} from "@/api/logistics/company";
import {listPostman, getPostman, delPostman, addPostman, updatePostman} from "@/api/logistics/postman";
import {listShop, getShop, delShop, addShop, updateShop} from "@/api/shop/shop";
import {listProduct, getProduct, delProduct, addProduct, updateProduct} from "@/api/product/product";
import {
  listUser,
  getUser,
  delUser,
  addUser,
  updateUser,
  resetUserPwd,
  changeUserStatus,
  deptTreeSelect
} from "@/api/system/user";
import {listCoupon, getCoupon, delCoupon, addCoupon, updateCoupon} from "@/api/cart/coupon";
import Dict from "../../system/dict";
import {listRegionCenter} from "@/api/logistics/region";

export default {
  name: "Logistics",
  components: {Dict},
  dicts: ['log_status_type', 'log_transit_status_type'],
  data() {
    return {
      /*物流提交表时间组件数据*/
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date());
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24);
            picker.$emit('pick', date);
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', date);
          }
        }]
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedInfo: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物流信息表格数据
      logisticsList: [],
      // 物流详细信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        logsNumber: null,
        companyId: null,
        postmanId: null,
        status: null,
        shippingAddress: null,
        deliveryAddress: null,
        address: null,
        deliveryTime: null,
        estimatedTime: null,
        actualTime: null
      },

      //查询对应数据id的详细信息
      query: {},
      //查询抽屉第二层数据的详细信息
      innerQuery: {},
      //是否显示抽屉
      drawer: false,
      //抽屉标题
      drawerTitle: null,
      //第二次抽屉的标题
      innerDrawerTitle: null,
      //是否显示第二次抽屉
      innerDrawer: false,

      /*级联选择器区域数组*/
      regions:[],

      /*物流公司数据*/
      companyList:[],
      /*快递员数据*/
      postmanList:[],

      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderId: [
          {required: true, message: "订单ID不能为空", trigger: "blur"}
        ],
        logsNumber: [
          {required: true, message: "物流单号不能为空", trigger: "blur"}
        ],
        companyId: [
          {required: true, message: "物流公司ID不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "物流状态不能为空", trigger: "change"}
        ],
        shippingAddress: [
          {required: true, message: "发货地址不能为空", trigger: "blur"}
        ],
        deliveryAddress: [
          {required: true, message: "收货地址不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 根据id查询物流公司*/
    getCompanyById(title, id) {
      getCompany(id).then(response => {
        console.log(response.data)
        if (response.data == null) {
          return
        }
        this.query = response.data
        this.drawerTitle = title + ""
        this.drawer = true
      })
    },
    /** 根据id查询订单*/
    getOrderById(title, id) {
      getOrder(id).then(response => {
        console.log(response.data)
        if (response.data == null) {
          return
        }
        this.query = response.data
        console.log(this.query)
        this.drawerTitle = title + ""
        this.drawer = true
      })
    },
    /** 根据id查询送件人*/
    getPostmanById(title, id) {
      getPostman(id).then(response => {
        console.log(response.data)
        if (response.data == null) {
          return
        }
        this.query = response.data
        this.drawerTitle = title + ""
        this.drawer = true
      })
    },
    /** 根据id查询商铺*/
    getShopsById(title, id) {
      getShop(id).then(response => {
        console.log(response.data)
        if (response.data == null) {
          return
        }
        this.innerQuery = response.data
        this.innerDrawerTitle = title + ""
        this.innerDrawer = true
      })
    },
    /** 根据id查询商品*/
    getProById(title, id) {
      console.log(id)
      getProduct(id).then(response => {
        console.log(response.data)
        if (response.data == null) {
          return
        }
        this.innerQuery = response.data
        this.innerDrawerTitle = title + ""
        this.innerDrawer = true
      })
    },
    /** 根据id查询用户*/
    getUserById(title, id) {
      getUser(id).then(response => {
        console.log(response.data)
        if (response.data == null) {
          return
        }
        this.innerQuery = response.data
        this.innerDrawerTitle = title + ""
        this.innerDrawer = true
      })
    },
    /** 根据id查询优惠券*/
    getCouponById(title, id) {
      getCoupon(id).then(response => {
        console.log(response.data)
        if (response.data == null) {
          return
        }
        this.innerQuery = response.data
        this.innerDrawerTitle = title + ""
        this.innerDrawer = true
      })
    },


    /** 查询物流信息列表 */
    getList() {
      this.loading = true;
      listLogistics(this.queryParams).then(response => {
        this.logisticsList = response.rows;
        console.log("this.logisticsList")
        console.log(this.logisticsList)
        this.total = response.total;
        this.loading = false;
      });
      listCompany().then(response => {
        this.companyList = response.rows
      })
      listPostman().then(response => {
        this.postmanList = response.rows
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderId: null,
        logsNumber: null,
        companyId: null,
        postmanId: null,
        status: null,
        shippingAddress: null,
        deliveryAddress: null,
        address: null,
        deliveryTime: null,
        estimatedTime: null,
        actualTime: null
      };
      this.infoList = [];
      this.regions = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物流信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      listRegionCenter(3).then(response => {
        this.regions=response.data
        console.log(this.regions)
        getLogisticsCascader(id).then(response => {
          this.form = response.data;
          console.log(this.form)
          this.infoList = response.data.infoList;
          this.infoList.forEach(item => {
            let c=[]
            if (item.currentAddressCenter!==null){
              c.push(item.currentAddressCenter.regionParent.region.id)
              c.push(item.currentAddressCenter.regionParent.id)
              c.push(item.currentAddressCenter.id)
              item.currentParent=c
            }
            if (item.toAddressCenter!==null){
              c=[]
              c.push(item.toAddressCenter.regionParent.region.id)
              c.push(item.toAddressCenter.regionParent.id)
              c.push(item.toAddressCenter.id)
              item.toParent=c
            }
          })
          this.open = true;
          this.title = "修改物流信息";
        });
      })

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.infoList = this.infoList;
          if (this.form.id != null) {
            updateLogistics(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLogistics(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除物流信息编号为"' + ids + '"的数据项？').then(function () {
        return delLogistics(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 物流详细信息序号 */
    rowInfoIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    /** 物流详细信息添加按钮操作 */
    handleAddInfo() {
      let obj = {};
      obj.logsNumber = "";
      obj.changeTime = "";
      obj.transitStatus = "";
      obj.toAddress = "";
      obj.currentAddress = "";
      obj.status = "";
      this.infoList.push(obj);
    },
    /** 物流详细信息删除按钮操作 */
    handleDeleteInfo() {
      if (this.checkedInfo.length == 0) {
        this.$modal.msgError("请先选择要删除的物流详细信息数据");
      } else {
        const infoList = this.infoList;
        const checkedInfo = this.checkedInfo;
        this.infoList = infoList.filter(function (item) {
          return checkedInfo.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleInfoSelectionChange(selection) {
      this.checkedInfo = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('logistics/logistics/export', {
        ...this.queryParams
      }, `logistics_${new Date().getTime()}.xlsx`)
    },
    handleCascaderChange(row, value) {
      // 提取选中的值，假设级联选择器返回的是一个数组
      const selectedValue = value[value.length - 1];
      // 将选中的值赋给scope.row.toAddress
      row.toAddress = selectedValue;
    }
  }
};
</script>
<style>
.drawer {
  margin: 6px;
}

.a1 th {
  width: 100px;
}
</style>
